package com.lovo.util;

import com.lovo.dao.IStudentDAO;
import com.lovo.model.StudentDO;
import com.lovo.model.UserDO;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SQLUtil {
    private static boolean addid=false;


    public static String getDelByIdSQL(Class c) {
        //delete from `petinfo` where pet_id = ?
        String sql = "delete from ";
        if(c.isAnnotationPresent(Table.class)){
            Table table = (Table)c.getAnnotation(Table.class);
            //得到Table注解中的表名
            sql += table.value()+" where ";
        }
        Field[] farray = c.getDeclaredFields();
        for (Field f :farray){
            if (f.isAnnotationPresent(Id.class) && f.isAnnotationPresent(Column.class)){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                sql +=columnname +" = #{" +f.getName()+
                        "}";
            }
        }

//        System.out.println(sql);
        return sql;
    }

    public static String getAddSQL(Class c) {
        String sql = "insert into ";
        //判断该类中是否有Table注解
        if(c.isAnnotationPresent(Table.class)){
            Table table = (Table)c.getAnnotation(Table.class);
            //得到Table注解中的表名
            sql += table.value()+"(";
        }
        String value = "";
        Field[] farray = c.getDeclaredFields();
        for (Field f :farray){
            //判断该属性是否有Column注解，并且没有Id注解
            if(f.isAnnotationPresent(Column.class) && f.isAnnotationPresent(Id.class) == false) {
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //在SQL语 句中，拼接列名
                sql += column.value()+",";
                value +="#{"+f.getName()+"},";
            }
        }
        value = value.substring(0,value.length() - 1);
        sql = sql.substring(0,sql.length() - 1);
        sql += ") values("+value+")";
//        System.out.println(sql);
        return sql;
    }




    public static String getLabelSQL(Class c) {
        //sql = sql.substring(0,sql.length() - 1);
        String sql = "";
        Field[] farray = c.getDeclaredFields();

        for (Field f :farray){

            if (f.isAnnotationPresent(Column.class)){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                //得到属性名
                String propertyname = f.getName();
                sql += columnname +" as " +propertyname +",\n";

            }
        }
        sql = sql.substring(0,sql.length() - 2);
//        System.out.println(sql);
        return sql;
    }



    public static String getFindByIdSQL(Class c) {
        //SELECT * FROM `petinfo` where pet_id = ?
        String sql = "SELECT * FROM ";
        if (c.isAnnotationPresent(Table.class)){
            Table table = (Table) c.getAnnotation(Table.class);
            String tableName = table.value();
            sql += tableName;
        }else {
            System.out.println("bean里没写Table注解");
            return null;
        }

        Field[] farray = c.getDeclaredFields();

        for (Field f :farray){
            if (f.isAnnotationPresent(Id.class) && f.isAnnotationPresent(Column.class)){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                sql +=" where "+columnname +" = #{" +f.getName()+
                        "}";
            }
        }

//        System.out.println(sql);
        return sql;




    }


    /**
     *
     * @param c
     */
    public static String getResultMapXml(Class c){

        String res = "<resultMap id=\"" + c.getSimpleName() +
                "\" type=";
        //resultMap 里 type的内容
        String type = c.getName();
        res += "\""+type +"\">\n";
        //得到类的属性列表
        Field[] farray = c.getDeclaredFields();

        for (Field f :farray){
            //判断该属性是否有Column注解，并且没有Id注解
            if (f.isAnnotationPresent(Column.class) && f.isAnnotationPresent(Id.class) == false){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                //得到属性名
                String propertyname = f.getName();
                res += " \t<result column=\"" + columnname+
                        "\" property=\"" + propertyname+
                        "\"/>\n";
            }else if (f.isAnnotationPresent(Id.class) && f.isAnnotationPresent(Column.class)){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                //得到属性名
                String propertyname = f.getName();
                res +="\t<id column=\"" +columnname+
                        "\" property=\"" +propertyname+
                        "\"/>\n";
            }
        }
        res += "</resultMap>";

//        System.out.println(res);
        return res;

    }
//    UPDATE student_1117
//    SET s_name= #{name} , s_age=#{age}
//    WHERE s_id = #{id}
    public static String getUpdate(Class c){//
        String sql = "UPDATE ";
        if (c.isAnnotationPresent(Table.class)){
            Table table = (Table) c.getAnnotation(Table.class);
            String tableName = table.value();
            sql += tableName+" set ";
        }else {
            System.out.println("bean里没写Table注解");
            return null;
        }

        Field[] farray = c.getDeclaredFields();
        String idName = "";
        String idcolumnname = "";
        for (Field f :farray){
            if (!f.isAnnotationPresent(Id.class) && f.isAnnotationPresent(Column.class)){
                //得到属性中写义的column注解
                Column column = (Column)f.getAnnotation(Column.class);
                //注解的的值是列名
                String columnname = column.value();
                sql +=columnname +" = #{" +f.getName()+
                        "}, ";
            }else {
                Column column = (Column)f.getAnnotation(Column.class);
                idName = f.getName();
                idcolumnname = column.value();
            }
        }
        sql = sql.substring(0,sql.length() - 2);
        sql += " where " +idcolumnname +" = #{" +idName+
                "} ";
//        System.out.println(sql);
        return sql;
    }
    public  static String getFindAll(Class c){
        String sql = "SELECT * FROM ";
        if (c.isAnnotationPresent(Table.class)){
            Table table = (Table) c.getAnnotation(Table.class);
            String tableName = table.value();
            sql += tableName;
        }else {
            System.out.println("bean里没写Table注解");
            return null;
        }
        return sql;
    }



    public static String getAllMapper(Class beanClass, Class interfaceClass ){
        String insertid = "";
        String deleteid = "";
        String updateid = "";
        String selectid = "";
        String selectallid ="";
        if (addid){
            Method[] methods = interfaceClass.getMethods();
            for (Method m :methods){
                String name = m.getName();
                if (name.matches("save.*") || name.matches("add.*")
                ||name.matches("insert.*") ){
                    insertid = name;
                }
                if (name.matches("remove.*") || name.matches("del.*")){
                    deleteid = name;
                }
                if (name.matches("update.*") ){
                    updateid = name;
                }
                if (name.matches("get.*") ){
                    selectid = name;
                }
                if (name.matches("list.*") ){
                    selectallid = name;
                }

            }
        }


        String res = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                "<!DOCTYPE mapper\n" +
                "        PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n" +
                "        \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n";
        res += "<mapper namespace=\"" + interfaceClass.getName()+
                "\">\n\n";
        res += getResultMapXml(beanClass)+"\n\n";

        res +="\t<insert id=\"" + insertid+
                "\" >\n\t\t"+getAddSQL(beanClass)+"\n\t</insert>\n\n" ;
        res +="\t<delete id=\"" + deleteid+
                "\" >\n\t\t"+getDelByIdSQL(beanClass)+"\n\t</delete>\n\n";
        res +="\t<update id=\"" + updateid+
                "\" >\n\t\t"+getUpdate(beanClass)+"\n\t</update>\n\n";
        res +="\t<select id=\"" + selectid+
                "\"  resultMap=\"" +beanClass.getSimpleName()+
                "\">\n\t\t"+getFindByIdSQL(beanClass)+"\n\t</select>\n\n";
        res +="\t<select id=\"" + selectallid+
                "\"  resultMap=\"" +beanClass.getSimpleName()+
                "\">\n\t\t"+getFindAll(beanClass)+"\n\t</select>\n\n";


        res +="</mapper>\n";
        return res;
    }
//    private void setaddid(boolean addid){
//
//    }
    public static String getAllMapper(Class beanClass, Class interfaceClass ,boolean addid){
        SQLUtil.addid = addid;
        return getAllMapper(beanClass, interfaceClass);

    }


    public static void main(String[] args) {
//        SQLUtil.getUpdate(StudentDO.class);
        System.out.println(SQLUtil.getAllMapper(StudentDO.class, IStudentDAO.class,true));

    }
}
